from copy import deepcopy
from Cube.utility.PythonObjectFileAccess import ObjectLoadFromFile, ObjectSaveToFile

class ConfigureAccess:
    def __init__(self, filePath, configInfo):
        self.ConfigureFilePath = filePath
        self.DefaultConfigure = configInfo
        try:
            self.CurrentConfigure = ObjectLoadFromFile(filePath)
        except:
            self.CurrentConfigure = deepcopy(configInfo)

    def ConfigureGet(self, key):
        item = self.CurrentConfigure.get(key,None)
        if item is not None:
            item = deepcopy(item)# return copy info
        else:
            item = self.DefaultConfigureGet(key)
            if item is not None:
                self.ConfigureSave({key:item})
        return item

    def ConfigureSave(self, infoDict = None):
        if infoDict is not None:
            self.CurrentConfigure.update(infoDict)
        ObjectSaveToFile(self.ConfigureFilePath, self.CurrentConfigure)

    def DefaultConfigureGet(self, key):
        item = self.DefaultConfigure.get(key,None)
        if item is not None:
            item = deepcopy(item)# return copy info
        return item
